#pragma once
#include <iostream>
#include <string>
#include <vector>
#include "InetAddr.hpp"
#include "Log.hpp"
using namespace LogModel;
class Route
{
    private:
    bool isExist(InetAddr& Id)
    {
        for(auto& user:_online_user)
        {
            if(user==Id)
            return true;
        }
        return false;
    }
    void addUser(InetAddr& Id)
    {
        LOG(LogLevel::INFO)<<"新增一个用户"<<Id.GetName();
        _online_user.push_back(Id);
    }
    public:
    Route()
    {}
    void MessageRoute(int sockfd,const std::string& message,InetAddr& Id)
    {
        if(!isExist(Id))//如果不存在就添加用户
        {
            addUser(Id);
        }

        //
        std::string send_message = Id.GetName()+": "+message;

        //给全部在线用户刷新聊天区
        for(auto& user:_online_user)
        {
            sendto(sockfd,send_message.c_str(),send_message.size(),0,(struct sockaddr*)&user.AddrGet(),sizeof(user.AddrGet()));
        }
    }
    ~Route()
    {}
    private:
    std::vector<InetAddr> _online_user;
};